<?php

namespace App\Http\Controllers\background;

use Aoding9\Laravel\Xlswriter\Export\Demo\UserExport;
use App\Models\Admin\Supplier;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class BalanceController
{

    public function Balance(Request $request){
        $id = $request -> post('supplier_id');
        if(empty($id)){
            return fails('请传参');
        }
        $res = Supplier::where('id','=',$id)->value('balance');
        return successly($res);
    }
    /** 易派余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getyipaimoney($app_id,$key){
        $url = 'http://api.365sup.cn/order/balance.action';
        $uid = $app_id;
        $data = [
            'businessId' => $uid,
            'sign' => md5($uid.$key)
        ];
        $res =  curlRequest($url,$data);
        $arr =  XmlToArr($res);
        return $arr['balance'];
    }

    /** 卡盟余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getkamengmoney($app_id,$key){
        $url = 'http://www.kashangwl.com/api/customer';
        $customer_id = $app_id;
        $timestamp = time();
        $data = [
            'customer_id' => $customer_id,
            'timestamp' => $timestamp,
        ];
        $str = '';
        foreach($data as $k=>$v){
            $str.=$k.$v;
        }
        $sign = md5($key.$str);
        $data['sign'] = $sign;
        $res =  curlRequest($url,$data);
        $arr = json_decode($res,true);
        return $arr['data']['balance'];
    }

    /** 交壹吧余额
     * @param $app_id
     * @param $key
     * @return float|int|string
     */
    public function getjiaoyimoney($app_id,$key){
        $url = 'http://api.91bak.com/mch/queryBalance.do';
        $data = [
            'merAccount'=>$app_id,
            'merRequestTime'=>date('Y-m-d H:i:s',time())
        ];
        $sign = md5('data='.json_encode($data).'&key='.$key);
        $info = [
            'data'=>json_encode($data),
            'sign'=>$sign
        ];

        $info1 = http_build_query($info);
        $res =curlRequest($url,$info1);
        $res_arr = json_decode($res,true);

        if($res_arr['resultCode'] == 0){
            return $res_arr['data']['balance']/1000;
        }else{
            return '查询错误';
        }
    }
    /***
     *  获取福禄的money
     */
    public function getfulumoney($app_id,$key){
        $url = 'https://openapi.fulu.com/api/getway';
        $data = fuluinfo([],time(),'fulu.user.info.get',$app_id);
        //签名步骤一：把字典json序列化
        $json = json_encode( $data, 320 );
        //签名步骤二：转化为数组
        $jsonArr = preg_split('/(?<!^)(?!$)/u', $json );
        //签名步骤三：排序
        sort( $jsonArr );
        //签名步骤四：转化为字符串
        $string = implode( '', $jsonArr );
        //签名步骤五：在string后加入secret
        $string = $string . $key;
        //签名步骤六：MD5加密
        $result_ = strtolower( md5( $string ) );
        $data['sign'] = $result_;
        $data = json_encode($data);
        $res =curl_post($url,$data);
        $res = json_decode($res,true);
        if($res['result']){
            $info = json_decode($res['result'],true);
            return $info['balance'];
        }else{
            return 0;
        }

    }

    /** 硕软余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getshuoyimoney($app_id,$key){
        $url = 'http://api.sohan.hk:50080/API';
        $info = [
            'action' => 'YE',//指定编码
        ];
        $info = json_encode($info);
        $data['agentAccount'] =$app_id;
        $data['sign'] = md5($info .$key);
        $info = json_decode($info, true);
        $data['busiBody'] = $info;
        $data = json_encode($data);
        $curl =srcurl($url,$data);
        return $curl['agentBalance'];
    }

    /** 橙券余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getchengquanmoney($app_id,$key){
        $url = 'https://api.chengquan.cn/user/balance/get';
        $info = [
            'timestamp' =>getMsecTime(),
            'app_id' => $app_id
        ];
        ksort($info);
        $data_url = urldecode(http_build_query($info));
        $md5_str = $data_url.'&key='.$key;
        $str = md5($md5_str);
        $sign = strtoupper($str);
        $info['sign'] = $sign;
        $res =curlcheng_post($url,$info);
        $res_data = json_decode($res,true);
        if(isset($res_data['data'])){
            return $res_data['data']['balance'];
        }else{
            return 0;
        }
    }

    /** 颜浩余额
     * @param $app_id
     * @param $key
     * @return float|int
     */
    public function getyanhaomoney($app_id,$key){
        $url = 'http://api.999sup.com/User/GetAccount';
        $info = [
            'UserId' => $app_id,
            'TimesTamp' =>  getMsecTime()
        ];
        ksort($info);
        $md5_str = $info['UserId'].$info['TimesTamp'].$key;
        $str = md5($md5_str);
        $sign = strtoupper($str);
        $info['sign'] = $sign;
        $res =  curlcheng_post($url,$info);
        $res_data = json_decode($res,true);
        return $res_data['Data'][0]['Balance']/10000;
    }

    /** 蓝色兄弟余额
     * @param $app_id
     * @param $keys
     * @return mixed
     */
    public function getlansemoney($app_id,$keys){
        $url = 'http://openapi.1688sup.com/recharge/info';
        $array = [
            'merchantId' => $app_id,
            'timeStamp' => time()
        ];
        ksort($array);
        $sign='';
        foreach ($array as $key => $value){
            $sign.=sprintf('%s=%s&',$key,$value);
        }
        $newsign = strtoupper(md5($sign.'key='.$keys));
        $array['sign'] = $newsign;
        $array['version'] = 1.0;
        $array['extendParameter'] = [];
        $res = curlcheng_post($url,$array);
        $res_data = json_decode($res,true);
        return $res_data['balance'];
    }

    /** 达尚余额
     * @param $arr
     * @return mixed
     */
    public function getdashangmoney($arr){
        $url = 'http://47.114.209.57/unicomAync/queryBalance.do';
        foreach($arr as $k => $v){
            $array = [
                'userId' => $v['supplier_num']
            ];
            ksort($array);
            // print_r($array);exit;
            $sign=$array['userId'];
            //echo $sign;exit;
            $newsign = md5($sign.$v['key']);
            $array['sign'] = $newsign;
            $res = XmlToArr(curlcheng_post($url,$array));
            unset($arr[$k]['supplier_num']);
            unset($arr[$k]['key']);
            $arr[$k]['balance'] = $res['balance']/1000;
        }
        return $arr;
    }
    /** 云金余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getyunjinmoney($app_id,$key){
        $url = 'http://openapi.yunjinshuke.com/api/gateway';
        $array = [
            'appKey'=>$app_id,
            'method' => 'account.query',
            'timestamp' => date('Y-m-d H:i:s',time()),
            'version' => '1.0'
        ];
        $recharge_array = [
        ];
        $array['reqParams'] = json_encode($recharge_array,JSON_FORCE_OBJECT);
        //签名步骤一：把字典json序列化
        $json = json_encode( $array, 320 );
        //签名步骤二：转化为数组
        $jsonArr = mb_str_split( $json );
        //签名步骤三：排序
        sort( $jsonArr );
        //签名步骤四：转化为字符串
        $string = implode( '', $jsonArr );
        //签名步骤五：在string后加入secret
        $string = $string . $key;
        //签名步骤六：MD5加密
        $sign = strtolower( md5( $string ) );
        $array['sign'] = $sign;
        $res = curl_post($url,json_encode($array));
        $res_data = json_decode($res,true);
        return $res_data['result']['balance'];
    }

    /** 子轩余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getzixuanmoney($app_id,$key){
        $url = 'https://120.79.157.192:8443/api/account/balance';
        list($usec, $sec) = explode(" ", microtime());

        $cn_time=date('YmdHis', time()).round($usec*1000);
        $array = [
            'appId'=>$app_id,
            'timestamp' => $cn_time,
            'appSecret'=>$key,//商户密钥
        ];
        ksort($array);
        $array['sign'] =  md5(urldecode(http_build_query($array)));
        //print_r($array);
        $res =zixuan_post($url,$array);
        $res_data = json_decode($res,true);
        return $res_data['balance'];
    }

    /**  娱尚余额
     * @param $app_id
     * @param $key
     * @return float|int
     */
    public function getyushangmoney($app_id,$key){
        $url = 'http://mbapiq.mbs.onfishes.com/Member/GetAccount';
        $array = [
            'AppKey'=>$app_id,
            'TimesTamp' =>getMsecTime(),
        ];
        ksort($array);
        $sign = '';
        foreach($array as $k => $v){
            $sign.=$v;
        }
        $sign.=$key;

        //echo $sign;
        $array['Sign'] = strtoupper(md5($sign));
        //print_r($array);
        // print_r($array);
        $res = curlcheng_post($url,$array);
        $res_data = json_decode($res,true);
        return $res_data['Data'][0]['Balance']/10000;
    }

    /** 微卡云余额
     * @param $app_id
     * @param $key
     * @param $url
     * @return mixed
     */
    public function getyunfakamoney($app_id,$key,$url){
        $param = [
            'userid'=>$app_id
        ];
        ksort($param); //排序post参数
        reset($param); //内部指针指向数组中的第一个元素
        $signtext='';
        foreach ($param AS $key => $val) { //遍历POST参数
            if ($val == '' || $key == 'sign') continue; //跳过这些不签名
            if ($signtext) $signtext .= '&'; //第一个字符串签名不加& 其他加&连接起来参数
            $signtext .= "$key=$val"; //拼接为url参数形式
        }
        $newsign=md5($signtext.$key);
        $array['sign'] = $newsign;
        $res = yunfacurl_https($url,$param);
        $res_data = json_decode($res,true);
        if($res_data){
            return $res_data['data']['money'];
        }else{
            return 0;
        }

    }

    /** 商盟余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getsmmoney($app_id,$key){
        $sign = strtoupper(md5($key));
        $url = 'http://Open.shangmeng.top/api/UserInfo/GetMemberInfo?userName='.$app_id.'&sign='.$sign;
        $result = file_get_contents($url);
        $res = json_decode($result,true);
        return $res['data']['balance'];
    }

    /** 乐娱余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getleyumoney($app_id,$key){
        $url = 'http://yyapi.5duang.com/ApiOrder/Getway';
        $array = [
            'app_id' => $app_id,
            'method' => 'hr.order.account',
            'timestamp' => date('Y-m-d H:i:s',time()),
            'version' => '1.0',
            'biz_content' => ''
        ];
        ksort($array);
        $sign = '';
        foreach($array as $k => $v){
            $sign.=$k.$v;
        }
        $sign.=$key;
        // echo $sign;exit;
        $new_sign = strtoupper(md5($sign));
        $array['sign'] = $new_sign;
        $result = json_decode(curlcheng_post($url,$array),true);
        return $result['data']['Balance'];
    }

    /** 裕寅旺余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getfuyuwangmoney($app_id,$key){
        $url = 'http://116.62.100.67:10186/plat/api/old/queryBalance';
        // echo $url."\r\n";
        $please = [
            'szAgentId' =>$app_id
        ];
        $str = 'szAgentId='.$please['szAgentId'].'&szKey='.$key;
        //echo $str."\r\n";
        $please['szVerifyString'] = md5($str);
        //print_r($please);
        ksort($please);
        //var_dump(curlcheng_post($url,$please));exit;
        $result = json_decode(curlcheng_post($url,$please),true);
        // if($result == null){
        //     return '接口请求失败';
        //     exit;
        // }
        return $result['fBalance'];
    }

    /**  百妙余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getbaimiaomoney($app_id,$key){
        $url = 'http://llcz.cloudbay.com.cn/flow/chbalance.action';
        $platform_id =$app_id;
        $array = [
            'timestamp' =>getMsecTime(),
            'security'=>$key,
            'platformid' => $platform_id
        ];
        ksort($array);
        $old_sign = urldecode(http_build_query($array));
        $new_sign = strtoupper(md5($old_sign));
        $array['sign'] = $new_sign;
        $header = [
            'Authorization: platformid="'.$platform_id.'"',
            'Accept:application/json;charset=UTF-8',
            'Content-Type:application/json'
        ];
        $res  = json_decode(baimiaorequest($url,json_encode($array),$header),true);
        return $res['totalmoney'];
    }

    /** 旧梦余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getjiumengmoney($app_id,$key){
        $url = 'http://www.haocard.vip/Order/queryBalance';
        // echo $url."\r\n";
        $please = [
            'merchantId' =>$app_id
        ];
        //print_r($please);
        ksort($please);
        $please['sign'] = md5($please['merchantId'].$key);
        $result = json_decode(curlcheng_post($url,$please),true);
        return $result['balance'];
    }

    /** 迅银余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getxunyinmoney($app_id,$key){
        $url = 'https://openapi.xunyin.com/openapi/QueryMerchant';
        $array = [
            'cpid' =>$app_id
        ];
        $array['sign'] = md5($array['cpid'].$key);
        $res =  XmlToArr( curlcheng_post($url,$array));
        return $res['balance'];
    }

    /** 乐冲余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getlechongmoney($app_id,$key){
        $url = 'http://121.196.177.34:1012/api/md5/getbalance';
        $array = [
            'agentcode' => $app_id,
            'time' => date('YmdHis'),
        ];
        $array['sign'] = md5($array['agentcode'].$array['time'].$key);
        $res_data = json_decode( curlcheng_post($url, $array),true);
        return $res_data['balance'];
    }

    /** 恒易迅余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function gethyxmoney($app_id,$key){
        $url = 'http://video.api.sctongqian.com/Business/QueryAmount';
        $array = [
            'AppKey' => $app_id,
            'TimesTamp' => time(),
            'Version' => '1.0'
        ];
        $array['sign'] = strtoupper(md5($array['AppKey'].$array['TimesTamp'].$array['Version'].$key));
        $res_data = json_decode( curlcheng_post($url, $array),true);
        //print_r($res_data);
        return $res_data['Data']['WalletBalance'];
    }

    /** 西柚余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getxymoney($app_id,$key){
        $url = 'http://shop.591mf.top/SubAPI/Query';
        $array = [
            'ID'=>$app_id
        ];
        ksort($array);
        $orsg = urldecode(http_build_query($array));
        $array['SG'] = strtoupper(md5($orsg));
        $res_data = json_decode(curlRequest($url, $array),true);
        return $res_data['Moeny'];

    }

    /** 胜始科技余额
     * @param $app_id
     * @param $key
     * @return float
     */
    public function getsskjmoney($app_id,$key){
        $url = 'http://open.hfshengshi.cn/api/user/balance';
        $data = [
            'app_id'=>$app_id,
            'timestamp'=>time()
        ];
        ksort($data);
        $sign_str = http_build_query($data).$key;
        $sign = strtoupper(md5($sign_str));
        $data['sign'] = $sign;
        $res = json_decode( curlRequest($url, $data),true);
        return round($res['balance'],4);
    }

    /** 昊天余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function gethaotian($app_id,$key){
        $url = 'https://api.haotian.chengmeiyouxuan.com/api.s/v1/balance';
        $param = [
            'business_number'=>$app_id,
        ];
        ksort($param);
        $sign_str = json_encode($param).$key;
        $sign = md5($sign_str);
        $param['sign']=$sign;
        $res = json_decode( curlRequest($url,$param),true);
        return $res['data']['balance'];
    }

    /** 雷哥余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getleigemoney($app_id,$key){
        $array = [
            'AppId'=>$app_id,
            'Timestamp'=>(int) getMsecTime()
        ];
        $array['sign'] = md5( asc_sort($array).'&AppSecret='.$key);
        $url =  'http://api.order.reegool.com/datum/account';
        $res_data =curlRequest($url, $array);
        $res_data = json_decode($res_data,true);
        if(isset($res_data['Data'])){
            return $res_data['Data']['Balance'];
        }else{
            return 0;
        }
    }

    /** 星权益余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function gexingqunayimoney($app_id,$key)
    {
        $url = 'http://xqy.api.xqy1.cn/api/customer';
        $customer_id = $app_id;
        $timestamp = time();

        $data = [
            'customer_id' => $customer_id,
            'timestamp' => $timestamp,
        ];
        $str = '';
        foreach($data as $k=>$v){
            $str.=$k.$v;
        }
        $sign =  md5($key.$str);
        $data['sign'] = $sign;
        $res =  curlRequest($url,$data);
        $arr = json_decode($res,true);
        return $arr['data']['balance'];
    }

    /**  海威余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function gehaiweimoney($app_id,$key){
        $url = 'http://test.api.jfshou.cn:11140/api/user/balance/get';
        $customer_id = $app_id;
        $timestamp =  getMsecTime();
        $arr = [
            'app_id' => $customer_id,
            'timestamp' => $timestamp,
        ];
        ksort($arr);
        $str = http_build_query($arr).'&key='.$key;
        $sign = strtoupper(md5($str));
        $arr['sign'] = $sign;
        $res =  curlRequest($url,$arr);
        $arr = json_decode($res,true);
        return $arr['data']['balance'];
    }

    /** 天天运通公司（深圳佰讯）余额
     * @param $app_id
     * @param $key
     * @return mixed
     */
    public function getttamoney($app_id,$key){
        $array = [
            'cid'=>$app_id,//商户号

        ];
        $array['sign'] =  md5($array['cid'].$key);
        $url = 'http://api.charge.tth5.cn/capi/balance.do';
        $res_data =   curlRequest($url, $array);
        $res = json_decode($res_data,true);
        return $res['balance'];
    }

    /** 工匠人心余额
     * @param $app_id
     * @param $key
     * @return void
     */
    public function getgongjiangmoney($app_id,$key){
        $array = [
            'business_id'=>$app_id,//商户号
        ];
        ksort($array);
        $array['sign'] =  md5(json_encode($array).$key);
        $url = 'http://xuni.chengmeiyoupin.com/api/SupplierAccount/getyourbalance';
        $res_data =   curlRequest($url, $array);
        $res = json_decode($res_data,true);
        Log::channel('task')->info($res);
        return $res['data'];
    }
}
